package com.roomio.domain;


import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.roomio.core.model.BaseObject;
import com.roomio.core.security.model.User;

@NamedQueries(value={
		@NamedQuery(name="group.by.name.and.username2",query=" select ug from UserGroup ug where id.group.name=:name and id.user.email=:userName ")
})

@Entity
@Table (name="USER_GROUP")
public class UserGroup extends BaseObject {
	

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	private Long id;


    private Boolean connected = false;

    @ManyToOne(fetch = FetchType.EAGER, cascade= { CascadeType.REFRESH })
	@JoinColumn(name = "USER_ID", nullable=false)
    private User user;

	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumn(name = "GROUP_ID", nullable=false)
    private Group group;
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((group == null) ? 0 : group.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((user == null) ? 0 : user.hashCode());
		return result;
	}


	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		UserGroup other = (UserGroup) obj;
		if (group == null) {
			if (other.group != null)
				return false;
		} else if (!group.equals(other.group))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (user == null) {
			if (other.user != null)
				return false;
		} else if (!user.equals(other.user))
			return false;
		return true;
	}


	

	public Long getId() {
		return id;
	}


	public void setId(Long id) {
		this.id = id;
	}


	public Boolean getConnected() {
		return connected;
	}


	public void setConnected(Boolean connected) {
		this.connected = connected;
	}
    
	public User getUser() {
		return user;
	}

	public void setUser(User userId) {
		this.user = userId;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group groupId) {
		this.group = groupId;
	}
    
}
